package com.bdqn.oa.dao.impl;

import com.bdqn.oa.dao.PositionRoleDao;
import com.bdqn.oa.entity.PositionRole;
import com.bdqn.oa.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author zhowei
 * @date 2022/7/20 17:56
 */
public class PositionRoleDaoImpl implements PositionRoleDao {
private QueryRunner queryRunner = new QueryRunner();
    /**
     * 添加用户权限
     *
     * @param positionRole
     * @return
     */
    @Override
    public int insertRole(PositionRole positionRole) throws SQLException {
        return queryRunner.update(DruidUtils.getConnection(),"insert into position_role (role_id,position_id)value(?,?)"
        ,positionRole.getRoleId(),positionRole.getPositionId());
    }

    /**
     * 解除权限
     *
     * @param positionRole
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteRole(PositionRole positionRole) throws SQLException {
        return queryRunner.update(DruidUtils.getConnection(),"delete from position_role where role_id=? and position_id=?",
                positionRole.getRoleId(),positionRole.getPositionId());
    }

    /**
     * 查看某岗位下的权限
     *
     * @param PoId
     * @return
     * @throws SQLException
     */
    @Override
    public List<PositionRole> getRoleByPoId(Integer PoId) throws SQLException {
        return queryRunner.query(DruidUtils.getConnection(),"select * from position_role where position_id=?",new BeanListHandler<PositionRole>(PositionRole.class),PoId);
    }
}
